<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUserAddressRequest;
use App\Http\Requests\UpdateUserAddressRequest;
use App\Models\UserAddress;
use Illuminate\Support\Facades\Auth;

class UserAddressController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $address = new UserAddress();
        $this->authorize('view', $address);
        $data = $address->where('user_id', Auth::id())->with('users')->orderBy('created_at', 'DESC')->get();

        return $this->success('查询成功', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreUserAddressRequest $request)
    {
        $userAddress = new UserAddress();
        $userAddress->fill($request->input());
        $userAddress->user_id = Auth::id();
        $userAddress->status = '1';
        $userAddress->order = 1;

        if ($request->input('is_default') === '1') {
            $user = Auth::user();
            // 找到当前默认地址并将其标记为非默认
            $user->addresses()->where('is_default', '1')->update(['is_default' => '2']);
            // 将新的地址标记为默认地址
            $userAddress->is_default = '1';
        }
        $userAddress->save();
        return $this->success('添加地址成功', $userAddress);
    }

    /**
     * Display the specified resource.
     */
    public function show(UserAddress $userAddress, $id)
    {
        $userAddress = $userAddress->findOrFail($id);
        $data = $userAddress->load('users');
        return $this->success('查询成功', $data);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(UserAddress $userAddress)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateUserAddressRequest $request, UserAddress $userAddress, $id)
    {
        $userAddress = $userAddress->findOrFail($id);
        $this->authorize('update', $userAddress);
        try {
            $userAddress->fill($request->input());
            $userAddress->user_id = Auth::id();
            // 获取当前用户
            if ($request->input('is_default') === '1') {
                $admin = Auth::user();
                // 找到当前默认地址并将其标记为非默认
                $admin->addresses()->where('is_default', '1')->update(['is_default' => '2']);
                // 将新的地址标记为默认地址
                $userAddress->is_default = '1';
            }

            $userAddress->save();

            return $this->success('更新地址成功', $userAddress);
        } catch (\Throwable $th) {
            return $this->error('更新失败', $th);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(UserAddress $userAddress, $id)
    {
        $userAddress = $userAddress->findOrFail($id);
        $this->authorize('delete', $userAddress);
        $userAddress->delete();
        return $this->success('删除成功', $userAddress);
    }
}
